home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Graphismes / Bitmap / NIH Image 1.59 / Macros / Measurement Macros < prev    next >
Text File  |  1995-01-20  |  12KB  |  550 lines

  1. macro 'Display Calibration Table';
  2. {
  3. Stores 0-255(all possible gray values) in the User1 column
  4. and the 256 corresponding calibrated values in the User2 column.
  5. Max Measurements must be set to 256 or greater. Use the Export
  6. command to export the calibration table to a text file. The two
  7. columns will be identical if the image is not calibrated.
  8. }
  9. var
  10.   i:integer;
  11.   v:real;
  12. begin
  13.   RequiresVersion(1.44);
  14.   SetCounter(256);
  15.   SetUser1Label('value');
  16.   SetUser2Label('cvalue');
  17.   for i:=0 to 255 do begin
  18.     rUser1[i+1]:=i;
  19.     rUser2[i+1]:=cvalue(i);
  20.   end;
  21.   ShowResults;
  22. end;
  23.  
  24.  
  25. macro 'Measure and draw line [L]';
  26. var
  27.   x1,x2,y1,y2,width:integer;
  28. begin
  29.   GetLine(x1,y1,x2,y2,width);
  30.   if x1<0 then begin
  31.     PutMessage('This macro requires a straight line selection.');
  32.     exit;
  33.   end;
  34.   Measure;
  35.   Fill;
  36.   KillRoi;
  37. end;
  38.  
  39. macro 'Measure and Outline [M]';
  40. begin
  41.   Measure;
  42.   DrawBoundary;
  43.   DrawBoundary;
  44. end;
  45.  
  46.  
  47. macro 'Measure All';
  48. {Measures all currently open images using the current selection. There is}
  49. {an implied "Select All" if the active image doesn't have a selection.}
  50. var
  51.   i,left,top,width,height:integer;
  52. begin
  53.   ResetCounter;
  54.   for i:=1 to nPics do begin
  55.     SelectPic(i);
  56.     RestoreROI;
  57.     Measure;
  58.   end;
  59. end;
  60.  
  61.  
  62. macro 'Measure All from Disk';
  63. {
  64. Reads from disk and measures a set of images too large to simultaneously
  65. fit in memory. The image names names must be in the form '01', '02', etc.
  66. Before starting, open and outline the first image('01').
  67. }
  68. var
  69.   i,width,height:integer;
  70. begin
  71.   GetPicSize(width,height);
  72.   if width=0 then begin
  73.     PutMessage('Before running this macro, open and outline the first image("01") in the series.');
  74.     exit;
  75.   end;
  76.   ResetCounters;
  77.   Measure;
  78.   close;
  79.   for i:=2 to 1000 do begin
  80.     open(i:2);
  81.     RestoreROI;
  82.     Measure;
  83.     close;
  84.   end;
  85. end;
  86.  
  87.  
  88. macro 'Paste Results'
  89. {Use the Measure command, the ruler tool, or the pointing tool to}
  90. {make up to about 10 measurements, then use this macro to paste}
  91. {the results into the upper left corner of the window.}
  92. begin
  93.   SetFont('Monaco');
  94.   SetFontSize(9);
  95.   SetText('Plain; Align Left');
  96.   SetOption; {Copy headings}
  97.   CopyResults;
  98.   MakeRoi(-10,0,250,150);
  99.   Paste;
  100.   KillRoi;
  101.   ResetCounter;
  102. end;
  103.  
  104.  
  105. macro 'Measure Redirected and Label'
  106. begin
  107.   Redirect(true);
  108.   Measure;
  109.   Redirect(false);
  110.   MarkSelection;
  111.   RestoreRoi;
  112. end;
  113.  
  114.  
  115. macro 'Reset Measurement Options';
  116. {Resets the Options dialog box in the Analyze menu to the default settings.}
  117. begin
  118.   RequiresVersion(1.44);
  119.   SetOptions('Area; Mean');
  120.   Redirect(false);
  121.   LabelParticles(true);
  122.   OutlineParticles(false);
  123.   IgnoreParticlesTouchingEdge(false);
  124.   IncludeInteriorHoles(false);
  125.   WandAutoMeasure(false);
  126.   AdjustAreas(false);
  127.   SetParticleSize(1,999999);
  128.   SetPrecision(2);
  129. end;
  130.  
  131.  
  132. macro 'Set Threshold╔';
  133. var
  134.   lower,upper:integer;
  135. begin
  136.   lower:=GetNumber('Lower:',1);
  137.   upper:=GetNumber('Upper:',254);
  138.   SetDensitySlice(lower,upper);
  139. end;
  140.  
  141.  
  142. macro 'Measure Accumulated Perimeter[A]';
  143. {
  144. Measures perimeter and computes accumulated perimeter,
  145. storing it in the User1 column.
  146. }
  147. var
  148.   i:integer;
  149.   Total:real;
  150. begin
  151.   SetOptions('Area; Mean; Perimeter; User1');
  152.   SetUser1Label('Total');
  153.   Measure;
  154.   Total:=0;
  155.   for i:=1 to rCount do Total:=Total+rLength[i];
  156.   rUser1[rCount]:=Total;
  157.   UpdateResults;
  158. end;
  159.  
  160.  
  161. macro 'Count Black and White Pixels [B]';
  162. {
  163. Counts the number of black and white pixels in the current
  164. selection and stores the counts in the User1 and User2 columns.
  165. }
  166. begin
  167.   RequiresVersion(1.44);
  168.   SetUser1Label('Black');
  169.   SetUser2Label('White');
  170.   Measure;
  171.   rUser1[rCount]:=histogram[255];
  172.   rUser2[rCount]:=histogram[0];
  173.   UpdateResults;
  174. end;
  175.  
  176.  
  177. macro 'Compute Percent Black and White';
  178. {
  179. Computes the percentage of back and white pixels in the
  180. current selection. This macro only works with binary images.
  181. }
  182. var
  183.   nPixels,mean,mode,min,max:real;
  184. begin
  185.   RequiresVersion(1.44);
  186.   SetUser1Label('Black');
  187.   SetUser2Label('White');
  188.   Measure;
  189.   GetResults(nPixels,mean,mode,min,max);
  190.   rUser1[rCount]:=histogram[255]/nPixels;
  191.   rUser2[rCount]:=histogram[0]/nPixels;
  192.   UpdateResults;
  193.   if (histogram[0]+histogram[255])<>nPixels
  194.     then PutMessage('This macro requires a binary image.');
  195. end;
  196.  
  197.  
  198. macro 'Compute Area Percentage [P]';
  199. {
  200. Computes the percentage of foreground
  201. pixels in the current selection.
  202. }
  203. var
  204.   mean,mode,min,max:real;
  205.   i,lower,upper,fPixels,nPixels,count:integer;
  206. begin
  207.   RequiresVersion(1.50);
  208.   SetUser1Label('%');
  209.   Measure;
  210.   GetResults(nPixels,mean,mode,min,max);
  211.   GetThresholds(lower,upper);
  212.   if (lower=0) and (upper=0) and 
  213.      ((histogram[0]+histogram[255])<>nPixels)
  214.      then begin
  215.        PutMessage('This macro requires a binary or thresholded image.');
  216.        exit;
  217.      end;
  218.   if nPixels=0 then begin
  219.   end;
  220.   if (lower=0) and (upper=0) then begin
  221.     if nPixels=0
  222.       then rUser1[rCount]:=0
  223.       else rUser1[rCount]:=(histogram[255]/nPixels)*100;
  224.     UpdateResults;
  225.     exit;
  226.   end;
  227.   fPixels:=0;
  228.   nPixels:=0;
  229.   for i:=0 to 255 do begin
  230.     count:=histogram[i];
  231.     nPixels:=nPixels+count;
  232.     if (i>=lower) and (i<=upper)
  233.       then fPixels:=fPixels+count;
  234.   end;
  235.   rUser1[rCount]:=(fPixels/nPixels)*100;
  236.   UpdateResults;
  237. end;
  238.  
  239.  
  240. macro 'Compute Average and Total Area [T]';
  241. {
  242. Computes average and accumulated area and stores 
  243. the them in the Major and Minor Axis columns.
  244. }
  245. var
  246.   i:integer;
  247.   sum:real;
  248. begin
  249.   RequiresVersion(1.44);
  250.   SetUser1Label('Avg');
  251.   SetUser2Label('Total');
  252.   SetOptions('Area; User1; User2');
  253.   Measure;
  254.   sum:=0;
  255.   for i:=1 to rCount do sum:=sum+rArea[i];
  256.   rUser1[rCount]:=sum/rCount;
  257.   rUser2[rCount]:=sum;
  258.   UpdateResults;
  259. end;
  260.  
  261.  
  262. macro 'Measure Circularity';
  263. begin
  264.   SetUser1Label('Shape');
  265.   Measure;
  266.   rUser1[rCount]:=4*3.14159265*(rArea[rCount]/sqr(rLength[rCount]));
  267.   UpdateResults;
  268. end;
  269.  
  270.  
  271. macro 'Measure Sum of Pixel Values';
  272. begin
  273.   SetUser1Label('Mean*Area');
  274.   Measure;
  275.   rUser1[rCount]:=rMean[rCount]*rArea[rCount];
  276.   UpdateResults;
  277. end;
  278.  
  279. macro 'Draw XY Center';
  280. var
  281.   left,top,width,height,x,y:real;
  282. begin
  283.   RequiresVersion(1.44);
  284.   GetRoi(left,top,width,height);
  285.   if width=0 then begin
  286.     PutMessage('This macro requires a selection.');
  287.     exit;
  288.   end;
  289.   SaveState; {Invert Y status saved starting with V1.44b21}
  290.   InvertY(false);
  291.   SetForegroundColor(255); {black}
  292.   SetOptions('Area; Mean; X-Y Center'); {XY Center}
  293.   Measure;
  294.   KillRoi;
  295.   x:=rX[rCount];
  296.   y:=rY[rCount];
  297.   MoveTo(x-5,y);
  298.   LineTo(x+5,y);
  299.   MoveTo(x,y-5);
  300.   LineTo(x,y+5);
  301.   RestoreState;
  302. end;
  303.  
  304.  
  305.  
  306.  
  307. macro 'Compute Spatial Scale';
  308. var
  309.   scale:real;
  310. begin
  311.   MakeLineRoi(0,0,100,0);
  312.   Measure;
  313.   KillRoi;
  314.   Scale:=100/rLength[rCount];
  315.   if scale=1
  316.     then PutMessage('Image is not spatially calibrated')
  317.     else PutMessage('Scale=',scale:1:4,' pixels/unit');
  318. end;
  319.  
  320.  
  321. procedure StoreZeros;
  322. begin
  323.   Measure;
  324.   rArea[rCount]:=0;
  325.   rMean[rCount]:=0;
  326.   rStdDev[rCount]:=0;
  327.   rX[rCount]:=0;
  328.   rY[rCount]:=0;
  329.   rLength[rCount]:=0;
  330.   rMajor[rCount]:=0;
  331.   rMinor[rCount]:=0;
  332.   rAngle[rCount]:=0;
  333.   rUser1[rCount]:=0;
  334.   rUser2[rCount]:=0;
  335.   UpdateResults;
  336. end;
  337.  
  338. macro 'Store Break in Results [S]';
  339. {Stores a row of zeros in the results table.}
  340. begin
  341.   StoreZeros;
  342. end;
  343.  
  344. macro 'Compute Means';
  345. var
  346.   n,i:integer;
  347. begin
  348.   n:=rCount;
  349.   StoreZeros;
  350.   StoreZeros;
  351.   for i:=1 to n do begin
  352.     rArea[rCount]:=rArea[rCount]+rArea[i];
  353.     rMean[rCount]:=rMean[rCount]+rMean[i];
  354.     rStdDev[rCount]:=rStdDev[rCount]+rStdDev[i];
  355.     rX[rCount]:=rX[rCount]+rX[i];
  356.     rY[rCount]:=rY[rCount]+rY[i];
  357.     rLength[rCount]:=rLength[rCount]+rLength[i];
  358.     rMajor[rCount]:=rMajor[rCount]+rMajor[i];
  359.     rMinor[rCount]:=rMinor[rCount]+rMinor[i];
  360.     rAngle[rCount]:=rAngle[rCount]+rAngle[i];
  361.     rUser1[rCount]:=rUser1[rCount]+rUser1[i];
  362.     rUser2[rCount]:=rUser2[rCount]+rUser2[i];
  363.   end; 
  364.   rArea[rCount]:=rArea[rCount]/n;
  365.   rMean[rCount]:=rMean[rCount]/n;
  366.   rStdDev[rCount]:=rStdDev[rCount]/n;
  367.   rX[rCount]:=rX[rCount]/n;
  368.   rY[rCount]:=rY[rCount]/n;
  369.   rLength[rCount]:=rLength[rCount]/n;
  370.   rMajor[rCount]:=rMajor[rCount]/n;
  371.   rMinor[rCount]:=rMinor[rCount]/n;
  372.   rAngle[rCount]:=rAngle[rCount]/n;
  373.   rUser1[rCount]:=rUser1[rCount]/n;
  374.   rUser2[rCount]:=rUser2[rCount]/n;
  375.   UpdateResults;
  376. end;
  377.  
  378. macro 'Measure both Raw and Calibrated';
  379. {
  380. This macro is a variation of the Measure command that displays the number
  381. of pixels in User1 and uncalibrated(raw) mean density in User2. It takes
  382. advantage of the fact that GetResults always returns uncalibrated values.
  383. }
  384. var
  385.   nPixels,mean,mode,min,max:real;
  386. begin
  387.   SetUser1Label('Pixels');
  388.   SetUser2Labe2('Raw Mean');
  389.   Measure;
  390.   GetResults(nPixels,mean,mode,min,max);
  391.   rUser1[rCount]:=nPixels;
  392.   rUser2[rCount]:=mean;
  393.   UpdateResults;
  394. end;
  395.  
  396.  
  397. macro 'Mark Centers';
  398. {Replaces each object in the image with a single pixel.}
  399. var i:integer;
  400. begin
  401.    Duplicate('Center');
  402.    SetScale(0,'pixels');
  403.    AutoThreshold;
  404.    AnalyzeParticles;
  405.    SelectAll;
  406.    Clear;
  407.    For i:=1 to rCount do
  408.       PutPixel(rX[i],rY[i],255);
  409. end;
  410.  
  411. macro 'Density Slice [D]';
  412. var
  413.   t1,t2:integer;
  414. begin
  415.   GetThresholds(t1,t2);
  416.   if (t1=0) and (t2=0) 
  417.     then SetDensitySlice(255,255)
  418.     else SetDensitySlice(0,0);
  419. end;
  420.  
  421. macro 'Set Scale and Aspect Ratio';
  422. {
  423. Sets the spatial scale and aspect ratio to predefined
  424. values contained in an image names "scale". This image
  425. can be very small, say 20x10. The directory (folder) path
  426. in the open statement will probably have to be changed.
  427. }
  428. begin
  429.   open('hd400:image:scale');
  430.   PropagateSpatial;
  431.   Dispose;
  432. end;
  433.  
  434. macro 'Write Results to Text Window';
  435. {This is an example of how to save results in a text window.}
  436. var
  437.   year,month,day,hour,minute,second,dow:integer;
  438. begin
  439.   GetTime(year,month,day,hour,minute,second,dow);
  440.   Measure;
  441.   NewTextWindow('My Results');
  442.   writeln('Date=',year-1900:1,':',month:1,':',day:1);
  443.   writeln('Time=',hour:1,':'minute:1,':',second:1);
  444.   writeln('Area=',rArea[rCount]:1:3);
  445.   writeln('Mean=',rMean[rCount]:1:3);
  446. end;
  447.  
  448. macro 'Find Radial Distances';
  449. {Finds center to edge distances along radial lines and displays them in User1.} 
  450. var
  451.   RoiLeft,RoiTop,RoiWidth,RoiHeight:integer;
  452.   x1,y1,x2,y2,count,ppv:integer;
  453.   pi,angle,delta,min,max,scale:real;
  454.   line,i,nLines,radius,r:integer;
  455.   unit:string;
  456. begin
  457.   RequiresVersion(1.55);
  458.   SaveState;
  459.   GetRoi(RoiLeft,RoiTop,RoiWidth,RoiHeight);
  460.   if RoiWidth=0 then begin
  461.     PutMessage('Selection Required.');
  462.     exit;
  463.   end;
  464.   GetScale(scale,unit);
  465.   MoveRoi(-RoiLeft,-RoiTop);
  466.   KillRoi;
  467.   RestoreRoi;
  468.   SetForegroundColor(255);
  469.   SetBackgroundColor(0);
  470.   SetNewSize(RoiWidth,RoiHeight);
  471.   MakeNewWindow('Temp');
  472.   RestoreRoi;
  473.   SetOptions('X-Y Center');
  474.   Measure;
  475.   DrawBoundary;
  476.   KillRoi;
  477.   x1:=rX[rCount]*scale;
  478.   y1:=rY[rCount]*scale;
  479.   radius:=sqrt(sqr(x1)+sqr(y1));
  480.   r:=sqrt(sqr(RoiWidth-x1)+sqr(y1));
  481.   if r>radius then radius:=r;
  482.   r:=sqrt(sqr(RoiWidth-x1)+sqr(RoiHeight-y1));
  483.   if r>radius then radius:=r;
  484.   r:=sqrt(sqr(x1)+sqr(RoiHeight-y1));
  485.   if r>radius then radius:=r;
  486.   nLines:=GetNumber('Number of Radial Lines:',36);
  487.   pi:=3.14159;
  488.   delta:=2.0*pi/nLines;
  489.   angle:=0.0;
  490.   ResetCounter;
  491.   SetUser1Label('Dist.');
  492.   SetOptions('User1');
  493.   for line:=1 TO nLines do begin
  494.     x2:=x1+round(radius*cos(angle));
  495.     y2:=y1+round(radius*sin(angle));
  496.     MakeLineRoi(x1,y1,x2,y2);
  497.     GetPlotData(count,ppv,min,max);
  498.     Fill;
  499.     i:=count;
  500.     repeat
  501.       i:=i-1;
  502.     until (i<=0) or (PlotData[i]>0);
  503.     rUser1[line]:=i;
  504.     angle:=angle+delta;
  505.   end;
  506.   KillRoi;
  507.   if scale<>1 then
  508.     for i:=1 to nLines do rUser1[i]:=rUser1[i]/scale;
  509.   SetCounter(nLines);
  510.   RestoreState;
  511.   ShowResults;
  512. end;
  513.  
  514. Macro 'Copy Results to Clipboard with Headers';
  515. begin
  516.   SelectWindow('Results');
  517.   SetOption; Copy;
  518. end;
  519.  
  520. Macro 'Export Results with Headers';
  521. begin
  522.   SetExport('Measurements');
  523.   SetOption; Export('HD80:Image:Results');
  524. end;
  525.  
  526. macro 'Feret Dimensions [F]';
  527. var
  528.    xloc,yloc,width,height:integer;
  529. begin
  530.   SetUser1Label('X Feret');
  531.   SetUser2Label('Y Feret');
  532.   Measure;
  533.   GetRoi(xloc,yloc,width,height);
  534.   rUser1[rCount]:=width;
  535.   rUser2[rCount]:=height;
  536.   UpdateResults;
  537. end;
  538.  
  539. macro 'Bounding Rectangle';
  540. var
  541.    xloc,yloc,width,height:integer;
  542. begin
  543.    GetRoi(xloc,yloc,width,height);
  544.    ShowMessage('xmin=', xloc, '\ymin=', yloc,
  545.        '\xmax=', xloc+width-1, '\ymax=', yloc+height-1);
  546.  end;
  547.  
  548.  
  549.  
  550.